<?php

namespace App\Repositories\Home;

use App\Models\Admin\CompanyModel;
use App\Models\Admin\JobCompanyModel;
use App\Models\Admin\JobModel;
use App\Models\Admin\UserModel;
use App\Repositories\BaseRepository;

class JobRepository extends BaseRepository
{
    protected $job;
    protected $jobCom;
    protected $com;
    protected $user;

    public function __construct()
    {
        $this->job = new JobModel();
        $this->jobCom = new JobCompanyModel();
        $this->com = new CompanyModel();
        $this->user = new UserModel();
    }

    /**
     * 获取web岗位搜索条件
     */
    public function getWebConditions()
    {
        //岗位
        $jobs = $this->job->where('del',0)
            ->where('pid',0)->get();
        $jobArr = array();
        foreach ($jobs as $job) {
            $jobArr[] = [
                'jobId' => $job->id,
                'jobName' => $job->name,
            ];
        }
        //学历
        $edus = $this->jobCom->getEdus();
        $eduArr = array();
        foreach ($edus as $key => $edu) {
            $eduArr[] = [
                'eduId' => $key,
                'eduName' => $edu,
            ];
        }
        //年限
//        $years = $this->jobCom->getYears();
        $years = array(
            1 => '一年以内', '一至二年', '三至五年', '五年以上',
        );
        $yearArr = array();
        foreach ($years as $key => $year) {
            $yearArr[] = [
                'yearId' => $key,
                'yearName' => $year,
            ];
        }
        return [
            'jobList' => $jobArr,
            'eduList' => $eduArr,
            'yearList' => $yearArr,
        ];
    }

    /**
     * 获取web岗位列表
     * @param $jobId 岗位ID
     * @param $eduId 学历ID
     * @param $yearId 年限ID
     * @param $page 页码
     * @param $pageSize 每页记录数
     */
    public function getWebJobList($jobId,$eduId,$yearId,$page,$pageSize)
    {
        $query = $this->jobCom->where('del',0);
        //搜索条件判断
        if ($jobId) { $query = $query->where('job',$jobId); }
        if ($eduId) { $query = $query->where('education',$eduId); }
        if ($yearId) { $query = $query->where('work_year',$yearId); }
        //分页处理
        $limit = $pageSize ? $pageSize : $this->limit;
        $pageStart = ($page - 1) * $limit;
        $models = $query->skip($pageStart)->take($limit)->get();
        $dataArr = array();
        if (!count($models)) { return $dataArr; }
        foreach ($models as $model) {
            $comOne = $this->com->where('uid',$model->cid)->first();
            $jobId = $model ? $model->job : 0;
            $uid = $model ? $model->cid : 0;
            $jobOne = $this->job->where('id',$jobId)->first();
            $userOne = $this->user->where('id',$uid)->first();
            $jobName = $jobOne ? $jobOne->name : '未定义';
            $comName = $comOne ? $comOne->com_name : '未定义';
            $head = $userOne ? $userOne->head : '';
            $welfares = $this->jobCom->getWelfares();
            $edus = $this->jobCom->getEdus();
//            $workYears = $this->jobCom->getYears();
            $workYearName = $this->jobCom->getYears($model->work_year);
            $welfareName = array_key_exists($model->welfare,$welfares) ?
                $welfares[$model->welfare] : '未定义';
            $eduName = array_key_exists($model->education,$edus) ?
                $edus[$model->education] : '未定义';
//            $workYearName = array_key_exists($model->work_year,$workYears) ?
//                $workYears[$model->work_year] : '未定义';
            $restName = $model->rest==1 ? '单休' : '双休';
//            if ($model->age_range) {
//                $ages = explode(',',$model->age_range);
//            } else {
//                $ages = array(0,0);
//            }
            $dataArr[] = [
                'name' => $comName,
                'jobName' => $jobName,
                'head' => $head,
                'num' => $model->num,
                'emolumentName' => $model->emolument . '元',
                'welfareName' => $welfareName,
                'restName' => $restName,
                'eduName' => $eduName,
                'workYearName' => $workYearName,
                'address' => $model->address,
                'detail' => $model->detail,
                'workHour' => $model->work_hour,
                'contactName' => $model->contact_name,
                'contactTelephone' => $model->contact_telephone,
                'email' => $model->email,
                'ageRange' => $model->age_range,
                'skill' => $model->skill,
            ];
        }
        return $dataArr;
    }

    /**
     * 获取详情:
     */
    public function getOne($jobId)
    {
        $model = $this->jobCom->where('del',0)->where('id',$jobId)->first();
        $comOne = $this->com->where('uid',$model->cid)->first();
        $jobId = $model ? $model->job : 0;
        $uid = $model ? $model->cid : 0;
        $jobOne = $this->job->where('id',$jobId)->first();
        $userOne = $this->user->where('id',$uid)->first();
        $jobName = $jobOne ? $jobOne->name : '未定义';
        $comName = $comOne ? $comOne->com_name : '未定义';
        $head = $userOne ? $userOne->head : '';
        $welfares = $this->jobCom->getWelfares();
        $edus = $this->jobCom->getEdus();
//        $workYears = $this->jobCom->getYears();
        $workYearName = $this->jobCom->getYears($model->work_year);
        $welfareName = array_key_exists($model->welfare,$welfares) ?
            $welfares[$model->welfare] : '未定义';
        $eduName = array_key_exists($model->education,$edus) ?
            $edus[$model->education] : '未定义';
//        $workYearName = array_key_exists($model->work_year,$workYears) ?
//            $workYears[$model->work_year] : '未定义';
        $restName = $model->rest==1 ? '单休' : '双休';
        return array(
            'name' => $comName,
            'jobName' => $jobName,
            'head' => $head,
            'num' => $model->num,
            'emolumentName' => $model->emolument . '元',
            'welfareName' => $welfareName,
            'restName' => $restName,
            'eduName' => $eduName,
            'workYearName' => $workYearName,
            'address' => $model->address,
            'detail' => $model->detail,
            'workHour' => $model->work_hour,
            'contactName' => $model->contact_name,
            'contactTelephone' => $model->contact_telephone,
            'email' => $model->email,
            'ageRange' => $model->age_range,
            'skill' => $model->skill,
        );
    }
}